package com.yongoe.ecy.secondNode.controller;

import com.yongoe.ecy.firstNode.entity.FirstNode;
import com.yongoe.ecy.secondNode.service.SecondNodeService;
import com.yongoe.ecy.config.aop.WebLog;
import com.yongoe.ecy.secondNode.controller.vo.excel.SecondNodeExcel;
import com.yongoe.ecy.secondNode.controller.vo.req.SecondNodeReq;
import com.yongoe.ecy.secondNode.controller.vo.res.SecondNodeRes;
import com.yongoe.ecy.secondNode.convert.SecondNodeConvert;
import com.yongoe.ecy.secondNode.entity.SecondNode;
import com.yongoe.ecy.utils.ExcelUtils;
import com.yongoe.ecy.utils.PageUtils;
import com.yongoe.ecy.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 二级节点
 *
 * @author monad
 * @since 2024/03/10
 */
@Tag(name = "二级节点")
@RestController
@RequestMapping("/secondNode/secondnode")
public class SecondNodeController {
    @Resource
    private SecondNodeService secondNodeService;
    @Resource
    private SecondNodeConvert secondNodeConvert;

    //@WebLog
    @Operation(summary = "查询分页数据")
    @PostMapping("/page")
    public R page(@RequestBody SecondNodeReq secondNodeReq) {
        SecondNode entity = secondNodeConvert.req2Entity(secondNodeReq);
        Page<SecondNode> page = secondNodeService.getSecondNodeByPage(Page.of(secondNodeReq.getCurrent(), secondNodeReq.getSize()), entity);
        return R.success().put(new PageUtils(page));
    }

    @WebLog
    @Operation(summary = "添加二级节点")
    @PostMapping("/add")
    public R add(@RequestBody SecondNode secondNode) {
        Long nodeID = secondNodeService.addSecondNode(secondNode);
        secondNode.setNodeId(nodeID);
        secondNodeService.save(secondNode);
        return R.success("添加成功");
    }

    @WebLog
    @Operation(summary = "修改二级节点")
    @PostMapping("/update")
    public R update(@RequestBody SecondNode secondNode) {
        secondNodeService.updateSecondNode(secondNode);
        secondNode.setFirstNodeName(null);
        secondNodeService.updateById(secondNode);
        return R.success("修改成功");
    }

    @WebLog
    @Operation(summary = "删除二级节点")
    @PostMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        if (secondNodeService.selectChild(ids) != 0){
            return R.error("存在子节点，无法删除！");
        }
        secondNodeService.deleteSecondNodes(ids);
        secondNodeService.deleteByIds(ids);
        return R.success("删除成功");
    }

    // @WebLog
    // @Operation(summary = "导入数据")
    // @PostMapping("/upload")
    // public R upload(MultipartFile file) {
    //     List<SecondNodeExcel> excelList = ExcelUtils.upload(file, SecondNodeExcel.class);
    //     List<SecondNode> list = secondNodeConvert.excel2EntityList(excelList);
    //     secondNodeService.saveBatch(list);
    //     return R.success("导入成功");
    // }

    // @WebLog
    // @Operation(summary = "导出数据")
    // @GetMapping("/export")
    // public void export(SecondNodeReq req, HttpServletResponse response) {
    //     SecondNode entity = secondNodeConvert.req2Entity(req);
    //     Page<SecondNode> page = secondNodeService.getSecondNodeByPage(Page.of(-1, -1), entity);
    //     List<SecondNodeExcel> list = secondNodeConvert.entity2ExcelList(page.getRecords());
    //     ExcelUtils.export(response, list, SecondNodeExcel.class);
    // }
}